home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Dema / betonsoldier_spdemo.exe / {app} / Shaders / mesh_opa_lightmap.fx < prev    next >
Encoding:
Text File  |  2005-07-05  |  13.3 KB  |  438 lines

  1. //------------------------------------------------------------------------------------------------------
  2. //------------------------------------------------------------------------------------------------------
  3. //------------------------------------------------------------------------------------------------------
  4.  
  5. #include "shared.fx"
  6. #include "lighting.fx"
  7. #include "fog.fx"
  8.  
  9. //------------------------------------------------------------------------------------------------------
  10. //- Static parameters
  11. //------------------------------------------------------------------------------------------------------
  12.  
  13. texture                DiffuseMap;
  14. texture                LightMap;
  15.  
  16. // I must lure them with a new shared ambient param that wont be smashed by the engine.
  17. shared float4 LightMapAmbient; 
  18.  
  19. //------------------------------------------------------------------------------------------------------
  20. //------------------------------------------------------------------------------------------------------
  21. //------------------------------------------------------------------------------------------------------
  22.  
  23. struct    CVertexShaderInput
  24. {
  25.     float4    Position    :    POSITION;
  26.     float3    Normal        :    NORMAL;
  27.     float2    DiffuseUv    :    TEXCOORD0;    
  28.     float2    LightmapUv:    TEXCOORD1;
  29. };
  30.  
  31. //------------------------------------------------------------------------------------------------------
  32.  
  33. struct    CVertexShaderOutput
  34. {
  35.     float4    Position                :    POSITION;
  36.     float2    DiffuseUv                :    TEXCOORD0;    
  37.     float2    LightmapUv            :    TEXCOORD1;
  38.     FOG_OPTION_VERTEX_FIELD
  39. };
  40.  
  41. //------------------------------------------------------------------------------------------------------
  42.  
  43. struct    CVertexShaderOutputLighting
  44. {
  45.     float4    Position                :    POSITION;
  46.     float2    DiffuseUv                :    TEXCOORD0;    
  47.     float3    LightVector            :    TEXCOORD1;    
  48.     float3    NormalVector        :    COLOR0;    
  49.     float3    DiffuseColor        :    COLOR1;
  50. //    FOG_OPTION_VERTEX_FIELD
  51.     FOG_OPTION_VERTEX_FIELD_HACK(TEXCOORD2)
  52. };
  53.  
  54. //------------------------------------------------------------------------------------------------------
  55. //------------------------------------------------------------------------------------------------------
  56. //------------------------------------------------------------------------------------------------------
  57.  
  58. CVertexShaderOutput OpaLightmapGeomTech1Pass1(const CVertexShaderInput input);
  59. CVertexShaderOutputLighting OpaLightmapGeomTech2Pass1(const CVertexShaderInput input);
  60.  
  61. //------------------------------------------------------------------------------------------------------
  62. //------------------------------------------------------------------------------------------------------
  63. //------------------------------------------------------------------------------------------------------
  64.  
  65. CVertexShaderOutput    OpaLightmapGeomTech1Pass1 (const CVertexShaderInput input)
  66. {
  67.     CVertexShaderOutput output;
  68.     
  69.     // output position into world+view+projection space
  70.     output.Position = mul (input.Position,WorldCameraProjection);
  71.         
  72.     // Diffuse Uv output
  73.     output.DiffuseUv = input.DiffuseUv;
  74.         
  75.     // LightmapUv Uv output
  76.     output.LightmapUv = input.LightmapUv;
  77.         
  78.     // fog computation
  79.     FOG_OPTION_COMPUTE(output, output.Position);        
  80.     
  81.     return output;
  82. }
  83.  
  84. //------------------------------------------------------------------------------------------------------
  85. //------------------------------------------------------------------------------------------------------
  86. //------------------------------------------------------------------------------------------------------
  87.  
  88. CVertexShaderOutputLighting    OpaLightmapGeomTech2Pass1 (const CVertexShaderInput input)
  89. {
  90.     CVertexShaderOutputLighting output;
  91.     
  92.     // output position into world+view+projection space
  93.     output.Position = mul (input.Position,WorldCameraProjection);
  94.         
  95.     // Diffuse Uv output
  96.     output.DiffuseUv = input.DiffuseUv;
  97.         
  98.     // omnilight direction computation
  99.     float4 omniDirection = ComputeVectorAndLength (ObjectLocalLightPosition[0],input.Position);
  100.     
  101.     // omnilight direction attenuation
  102.     //omniDirection *= ComputeAttenuation (omniDirection.w,LightAttenuationFarStart[0],LightAttenuationFarEnd[0],LightAttenuationDelta[0]);
  103.     
  104.     output.DiffuseColor = DiffuseColor[0].xyz;
  105.     output.DiffuseColor *= ComputeAttenuation (omniDirection.w,LightAttenuationFarStart[0],LightAttenuationFarEnd[0],LightAttenuationDelta[0]);
  106.     
  107.     // omnilight output to diffuse
  108.     output.LightVector.xyz = omniDirection.xyz;
  109.     output.NormalVector.xyz = input.Normal * 0.5f + 0.5f;    
  110.     
  111.     // fog computation
  112.     FOG_OPTION(float rFog = fog_linear(output.Position); output.Hack = float4(rFog, rFog, rFog, rFog));
  113.     
  114.     return output;
  115. }
  116.  
  117. //------------------------------------------------------------------------------------------------------
  118. //------------------------------------------------------------------------------------------------------
  119. //------------------------------------------------------------------------------------------------------
  120.  
  121. sampler DiffuseSampler = sampler_state
  122. {
  123.     Texture        = <DiffuseMap>;
  124.     MinFilter    = LINEAR;
  125.     MagFilter    = LINEAR;
  126.     MipFilter    = LINEAR;
  127.     AddressU    = WRAP;
  128.   AddressV    = WRAP;
  129. };
  130.  
  131. //------------------------------------------------------------------------------------------------------
  132. //------------------------------------------------------------------------------------------------------
  133. //------------------------------------------------------------------------------------------------------
  134.  
  135. sampler LightTextureSampler = sampler_state
  136. {
  137.     Texture        = <LightTexture>;
  138.     MinFilter    = LINEAR;
  139.     MagFilter    = LINEAR;
  140.     MipFilter    = LINEAR;
  141.     AddressU    = CLAMP;
  142.   AddressV    = CLAMP;
  143. };
  144.  
  145. //------------------------------------------------------------------------------------------------------
  146. //------------------------------------------------------------------------------------------------------
  147. //------------------------------------------------------------------------------------------------------
  148.  
  149. sampler LightMapSampler = sampler_state
  150. {
  151.     Texture        = <LightMap>;
  152.     MinFilter    = LINEAR;
  153.     MagFilter    = LINEAR;
  154.     MipFilter    = LINEAR;
  155.     AddressU    = WRAP;
  156.   AddressV    = WRAP;
  157. };
  158.  
  159. //------------------------------------------------------------------------------------------------------
  160. //------------------------------------------------------------------------------------------------------
  161. //------------------------------------------------------------------------------------------------------
  162.  
  163. #define Pass1RenderStateBlock \
  164. CullMode            = CW; \
  165. ZEnable                = true; \
  166. ZFunc                = LESSEQUAL; \
  167. ZWriteEnable        = true; \
  168. AlphaBlendEnable    = false; \
  169. AlphaTestEnable        = true; \
  170. AlphaRef            = 192; \
  171. AlphaFunc            = GREATEREQUAL ; \
  172. FOG_OPTION_PARAMETERS
  173.  
  174. //------------------------------------------------------------------------------------------------------
  175.  
  176. #define LightingRenderStateBlock \
  177. CullMode            = CW; \
  178. ZEnable                = true; \
  179. ZFunc                = LESSEQUAL; \
  180. ZWriteEnable        = false; \
  181. AlphaBlendEnable    = true; \
  182. SrcBlend            = ONE; \
  183. DestBlend            = ONE; \
  184. AlphaTestEnable        = true; \
  185. AlphaRef            = 192; \
  186. AlphaFunc            = GREATEREQUAL ; \
  187. FogEnable            = false
  188.  
  189. //------------------------------------------------------------------------------------------------------
  190. //------------------------------------------------------------------------------------------------------
  191. //------------------------------------------------------------------------------------------------------
  192.  
  193. technique    tech1
  194. <
  195.     int Priority = 1;
  196.     int TechniqueIndex = 0;
  197.     int DeviceType = HWSHADER_ONLY;
  198.     int LightingType = ACCUMULATED_LIGHTING;
  199.     string RenderingType = "Standard";
  200. >
  201. {
  202.     pass pass1
  203.     {
  204.         Sampler[0]    = <DiffuseSampler>;
  205.         Sampler[1]    = <LightMapSampler>;
  206.         
  207.         Pass1RenderStateBlock;
  208.         
  209.         VertexShader = compile vs_1_1 OpaLightmapGeomTech1Pass1();
  210.         
  211.         PixelShader = 
  212.         asm
  213.         {
  214.             ps_1_1
  215.             
  216.             tex            t0    // Diffuse map
  217.             tex            t1    // Light map
  218.             
  219.             mul            r0.rgb,t1,t1.a + mov r0.a,t0.a
  220.             mul_x4    r0.rgb,r0,t0
  221.         };
  222.     }
  223. }
  224.  
  225. //------------------------------------------------------------------------------------------------------
  226. //------------------------------------------------------------------------------------------------------
  227. //------------------------------------------------------------------------------------------------------
  228.  
  229. technique    tech2
  230. <
  231.     int Priority = 1;
  232.     int TechniqueIndex = 0;
  233.     int DeviceType = HWSHADER_ONLY;
  234.     int LightingType = ACCUMULATED_LIGHTING;
  235.     string RenderingType = "Lighting";
  236. >
  237. {
  238.     pass pass1
  239.     {
  240.         Sampler[0]    = <DiffuseSampler>;
  241.         Sampler[1]    = <LightTextureSampler>;
  242.         
  243.         LightingRenderStateBlock;
  244.         
  245.         VertexShader = compile vs_1_1 OpaLightmapGeomTech2Pass1();
  246.         
  247.         PixelShader =
  248.         #ifdef FOG_OPTION_ENABLE
  249.         asm
  250.         {
  251.             ps_1_1
  252.             
  253.             tex            t0    // Diffuse map
  254.             tex            t1    // Renormalized light vector
  255.             texcoord    t2  // fog hack
  256.                         
  257.             dp3_sat r0.rgb,t1_bx2,v0_bx2 + mov r0.a,t0.a
  258.             
  259.             mul r0.rgb,r0,v1
  260.             mul_x2 r0.rgb,r0,t0
  261.             
  262.             // fog hack : colorFog = oldColor * (1.0f - FogDensity)
  263.             // in fact i do oldColor * 1.0f 
  264.             mul_sat r0.rgb, r0, t2
  265.         };
  266.         #else
  267.         asm
  268.         {
  269.             ps_1_1
  270.             
  271.             tex            t0    // Diffuse map
  272.             tex            t1    // Renormalized light vector
  273.                                 
  274.             dp3_sat r0.rgb,t1_bx2,v0_bx2 + mov r0.a,t0.a
  275.             
  276.             mul r0.rgb,r0,v1
  277.             mul_x2 r0.rgb,r0,t0
  278.         };
  279.         #endif
  280.     }
  281. }
  282.  
  283.  
  284. //------------------------------------------------------------------------------------------------------
  285. //------------------------------------------------------------------------------------------------------
  286. //------------------------------------------------------------------------------------------------------
  287.  
  288. technique    tech1Ambient
  289. <
  290.     int Priority = 1;
  291.     int TechniqueIndex = 1;
  292.     int DeviceType = HWSHADER_ONLY;
  293.     int LightingType = ACCUMULATED_LIGHTING;
  294.     string RenderingType = "Standard";
  295. >
  296. {
  297.     pass pass1
  298.     {
  299.         Sampler[0]    = <DiffuseSampler>;
  300.         Sampler[1]    = <LightMapSampler>;
  301.         
  302.         Pass1RenderStateBlock;
  303.         
  304.         VertexShader = compile vs_1_1 OpaLightmapGeomTech1Pass1();
  305.  
  306.         PixelShaderConstant[0]        = <LightMapAmbient>;
  307.         
  308.         PixelShader = 
  309.         asm
  310.         {
  311.             ps_1_1
  312.             
  313.             tex            t0    // Diffuse map
  314.             tex            t1    // Light map
  315.             
  316.             mul            r0.rgb,t1,t1.a + mov r0.a,t0.a
  317.             mul_x4    r0.rgb,r0,t0
  318.             mul            r0.rgb,r0,c0        
  319.         };
  320.     }
  321. }
  322.  
  323. //------------------------------------------------------------------------------------------------------
  324. //------------------------------------------------------------------------------------------------------
  325. //------------------------------------------------------------------------------------------------------
  326.  
  327. technique    tech2Ambient
  328. <
  329.     int Priority = 1;
  330.     int TechniqueIndex = 1;
  331.     int DeviceType = HWSHADER_ONLY;
  332.     int LightingType = ACCUMULATED_LIGHTING;
  333.     string RenderingType = "Lighting";
  334. >
  335. {
  336.     pass pass1
  337.     {
  338.         Sampler[0]    = <DiffuseSampler>;
  339.         Sampler[1]    = <LightTextureSampler>;
  340.         
  341.         LightingRenderStateBlock;
  342.         
  343.         VertexShader = compile vs_1_1 OpaLightmapGeomTech2Pass1();
  344.         
  345.         PixelShader =
  346.         #ifdef FOG_OPTION_ENABLE
  347.         asm
  348.         {
  349.             ps_1_1
  350.             
  351.             tex            t0    // Diffuse map
  352.             tex            t1    // Renormalized light vector
  353.             texcoord    t2  // fog hack
  354.                         
  355.             dp3_sat r0.rgb,t1_bx2,v0_bx2 + mov r0.a,t0.a
  356.             
  357.             mul r0.rgb,r0,v1
  358.             mul_x2 r0.rgb,r0,t0
  359.             
  360.             // fog hack : colorFog = oldColor * (1.0f - FogDensity)
  361.             // in fact i do oldColor * 1.0f 
  362.             mul_sat r0.rgb, r0, t2
  363.         };
  364.         #else
  365.         asm
  366.         {
  367.             ps_1_1
  368.             
  369.             tex            t0    // Diffuse map
  370.             tex            t1    // Renormalized light vector
  371.                                 
  372.             dp3_sat r0.rgb,t1_bx2,v0_bx2 + mov r0.a,t0.a
  373.             
  374.             mul r0.rgb,r0,v1
  375.             mul_x2 r0.rgb,r0,t0
  376.         };
  377.         #endif
  378.     }
  379. }
  380.  
  381. //------------------------------------------------------------------------------------------------------
  382. //------------------------------------------------------------------------------------------------------
  383. //------------------------------------------------------------------------------------------------------
  384.  
  385. technique    techTnL_0
  386. <
  387.     int Priority = 1;
  388.     int TechniqueIndex = 0;
  389.     int DeviceType = TNL_ONLY;
  390.     int LightingType = ACCUMULATED_LIGHTING;
  391.     string RenderingType = "Standard";
  392. >
  393. {
  394.     pass pass1
  395.     {
  396.         WorldTransform[0]    = <WorldCameraProjection>;
  397.         IndexedVertexBlendEnable = false;
  398.         
  399.         Sampler[0]    = <LightMapSampler>;
  400.         Sampler[1]    = <DiffuseSampler>;
  401.         
  402.         Pass1RenderStateBlock;
  403.         
  404.         TexCoordIndex[0]    = 1;
  405.         TexCoordIndex[1]    = 0;
  406.         TextureFactor        = {0.0f,0.0f,0.0f,0.0f};
  407.         
  408.         ColorArg1[0]    = TEXTURE;
  409.         ColorArg2[0]    = TFACTOR;
  410.         ColorOp[0]        = BLENDTEXTUREALPHA;
  411.         AlphaArg1[0]    = TEXTURE;
  412.         AlphaOp[0]        = SELECTARG1;
  413.         
  414.         ColorArg1[1]    = TEXTURE;
  415.         ColorArg2[1]    = CURRENT;
  416.         AlphaArg1[1]    = TEXTURE;
  417.         AlphaOp[1]        = SELECTARG1;
  418.         ColorOp[1]        = MODULATE4X;
  419.         
  420.         ColorOp[2]        = DISABLE;
  421.         AlphaOp[2]        = DISABLE;
  422.                     
  423.         VertexShader         = NULL;        
  424.         PixelShader         = NULL;
  425.     }
  426. }
  427.  
  428. //------------------------------------------------------------------------------------------------------
  429. //------------------------------------------------------------------------------------------------------
  430. //------------------------------------------------------------------------------------------------------
  431.  
  432. #include "mesh_shadow.fx"
  433. #include "mesh_shadow_projector.fx"
  434.  
  435. //------------------------------------------------------------------------------------------------------
  436. //------------------------------------------------------------------------------------------------------
  437. //------------------------------------------------------------------------------------------------------
  438.